package org.edu.modules.stu.stu.controller;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.edu.common.dto.ExcelPo;
import org.edu.common.system.vo.LoginUser;
import org.edu.common.util.oConvertUtils;
import org.edu.common.utils.PoiUtils;
import org.edu.modules.stu.common.entity.VeBaseAppUser;
import org.edu.modules.stu.common.service.VeBaseAppUserService;
import org.edu.modules.stu.common.service.VeBaseTeacherService;
import org.edu.modules.stu.stu.dto.VeStuStipendDto;
import org.edu.common.dto.AllDto;
import org.edu.modules.stu.stu.entity.VeStuStipend;
import org.edu.modules.stu.stu.service.VeStuStipendService;
import org.edu.common.api.vo.Result;
import org.edu.common.base.PageDto;
import org.edu.common.aspect.annotation.AutoLog;
import org.edu.common.system.query.QueryGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Api(tags = {"助学金信息表"})
@RestController
@RequestMapping("/stu/veStuStipend")
@Slf4j
public class VeStuStipendController{

    @Autowired
    private VeStuStipendService veStuStipendService;

    @Autowired
    private VeBaseAppUserService veBaseAppUserService;

    @Autowired
    private VeBaseTeacherService veBaseTeacherService;

    @ApiOperation("分页查询助学金信息表")
    @PostMapping("/page")
    public Result<?> page(@RequestBody PageDto<VeStuStipend> page) {
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if (oConvertUtils.isEmpty(loginUser)) {
            return Result.error("请登录系统！");
        }
        //根据用户id查用户信息
        VeBaseAppUser appUser = veBaseAppUserService.getVeBaseAppUserById(loginUser.getId());
        IPage<VeStuStipendDto> pageDto = null;
        QueryWrapper<VeStuStipend> queryWrapper =  page.buildQueryWrapper(VeStuStipend.class);
        if (appUser.getUserType().contains("2")) {
            //如果用户类型是2，为学生账号
            queryWrapper.eq("s.xh", appUser.getUserId());
            pageDto = veStuStipendService.pageDto(page, queryWrapper);
            return Result.OK(pageDto);
        }
        else if (appUser.getUserType().contains("1")) {
            //如果用户类型是1，为教师账号
            List<Integer> studentIdsList = veBaseTeacherService.getStudentIdsListBySysUserId(appUser.getUserId());

            queryWrapper.in("d.stu_id", studentIdsList);
            pageDto = veStuStipendService.pageDto(page, queryWrapper);
            return Result.OK(pageDto);
        } else {
            //查全部
            pageDto = veStuStipendService.pageDto(page, queryWrapper);
        }
        return Result.OK(pageDto);
    }

    /**
     * 查询所有
     */
    @ApiOperation("查询所有")
    @PostMapping("/all")
    public Result<?> all(@RequestBody AllDto<VeStuStipend> page) {
        QueryWrapper<VeStuStipend> queryWrapper = page.buildQueryWrapper(VeStuStipend.class);
        List<VeStuStipend> list = veStuStipendService.listByWrapper(queryWrapper);
        return Result.OK(list);
    }

    @ApiOperation("导出数据）")
    @PostMapping("/export")
    public Result<?> export(@RequestBody PageDto<VeStuStipend> page, HttpServletResponse resp) {
        QueryWrapper<VeStuStipend> queryWrapper =  page.buildQueryWrapper(VeStuStipend.class);
        List<ExcelPo> list = new ArrayList<>();
        list.add(new ExcelPo("学生姓名","xm"));
        list.add(new ExcelPo("学生姓名","xh"));
        list.add(new ExcelPo("班级","bjText"));
        list.add(new ExcelPo("助学金类型","typeName"));
        list.add(new ExcelPo("申请时间","applyTime"));
        list.add(new ExcelPo("审核状态","auditStatus","xjAuditStatus"));
        list.add(new ExcelPo("申请时间","giveTime"));
                PoiUtils.downloadFile(resp,veStuStipendService.pageDto(page, queryWrapper).getRecords(),list);
        return Result.OK();
    }

    @AutoLog(value = "助学金信息表-分页列表查询")
    @ApiOperation(value="助学金信息表-分页列表查询", notes="助学金信息表-分页列表查询")
    @PostMapping(value = "/list")
    public Result<?> queryPageList(VeStuStipend entity,
                                   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<VeStuStipend> queryWrapper = QueryGenerator.initQueryWrapper(entity, req.getParameterMap());
        PageDto<VeStuStipend> page = new PageDto<VeStuStipend>(pageNo, pageSize);
        IPage<VeStuStipend> pageList = veStuStipendService.page(page, queryWrapper);
        return Result.OK(pageList);
    }


    @AutoLog(value = "通过id查询")
    @ApiOperation(value="通过id查询", notes="通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
        VeStuStipend dto = veStuStipendService.findDtoById(id);
        if(dto==null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(dto);
    }

    @AutoLog(value = "添加")
    @ApiOperation(value="添加", notes="添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody VeStuStipendDto dto) {
        veStuStipendService.insertOrUpdate(dto , true);
        return Result.OK("添加成功！");
    }

    @AutoLog(value = "编辑")
    @ApiOperation(value="编辑", notes="编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody VeStuStipendDto dto) {
        veStuStipendService.insertOrUpdate(dto , false);
        return Result.OK("编辑成功!");
    }

    @AutoLog(value = "批量删除")
    @ApiOperation(value="批量删除", notes="批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
        veStuStipendService.delete(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }



    @AutoLog(value = "审核")
    @ApiOperation(value="审核")
    @PostMapping(value = "/audit")
    public Result<?> audit(@RequestBody VeStuStipendDto dto) {
        LambdaUpdateWrapper<VeStuStipend> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(VeStuStipend::getAuditStatus,dto.getAuditStatus());
        updateWrapper.set(VeStuStipend::getAuditTime,new Date());
        updateWrapper.set(VeStuStipend::getAuditReason,dto.getAuditReason());

        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        updateWrapper.set(VeStuStipend::getAuditUserId,sysUser.getId());
        updateWrapper.set(VeStuStipend::getAuditUserName,sysUser.getRealname());
        if(dto.getIds()!=null || dto.getIds().size()>0){
            updateWrapper.in(VeStuStipend::getId,dto.getIds());
        }else{
            updateWrapper.eq(VeStuStipend::getId,dto.getId());
        }
        veStuStipendService.update(updateWrapper);
        return Result.OK("审核成功!");
    }





}
